//
//  LockPassView.m
//  Monosaccharides
//
//  Created by iSolar on 2017/11/15.
//  Copyright © 2017年 nothing. All rights reserved.
//

#import "LockPassView.h"

#define lockButtonCount 9
#define columeCount 3
#define lineCount 3

@interface LockPassView()

//解锁按钮的宽高
@property (nonatomic, assign) CGFloat lockBUttonWH;

/**  路径  */
@property (nonatomic, strong) UIBezierPath *path;

/**    */
@property (nonatomic, strong) NSMutableString *buttonValue;

@property (nonatomic, assign) CGPoint movePoint;//当前移动的point

@property (nonatomic, assign) CGPoint selectedMovePoint;//z=最后一个被选中的按钮

@end

@implementation LockPassView

- (NSMutableString *)buttonValue {
    
    if (!_buttonValue) {
        _buttonValue = [NSMutableString string];
    }
    
    return _buttonValue;
}

- (void)awakeFromNib {
    
    [super awakeFromNib];
    self.lockBUttonWH = 80;
    [self initGestureButtonsView];
}

- (void)initGestureButtonsView {
    
    CGFloat marginX = (self.frame.size.width - columeCount * self.lockBUttonWH)/(columeCount + 1);
    CGFloat marginY = (self.frame.size.height - lineCount * self.lockBUttonWH) / (lineCount + 1);
    
    for (int i = 0; i < lockButtonCount; i ++) {
        
        // 计算按钮的X,Y值
        CGFloat buttonX = marginX + i % lineCount *(self.lockBUttonWH + marginX);
        CGFloat buttonY = marginY + i /columeCount * (self.lockBUttonWH + marginY);
        UIButton *lockButton = [[UIButton alloc] initWithFrame:CGRectMake( buttonX, buttonY, self.lockBUttonWH, self.lockBUttonWH)];
        lockButton.tag = i;
        [lockButton setImage:[UIImage imageNamed:@"gesture_node_normal"] forState:UIControlStateNormal];
        [lockButton setImage:[UIImage imageNamed:@"gesture_node_highlighted"] forState:UIControlStateSelected];
        
        lockButton.userInteractionEnabled = NO;
        NSLog(@"---%f----%f-----",buttonX,buttonY);
        [self addSubview:lockButton];
    }
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    UITouch *touch = touches.anyObject;
    CGPoint touchPoint = [touch locationInView:self];
    UIBezierPath *path = [UIBezierPath bezierPath];
    
    //每次按下时清空上次获取的密码
    self.buttonValue = nil;
    for (UIButton *btn in self.subviews) {
        if (CGRectContainsPoint(btn.frame, touchPoint)) {
            path.lineWidth = 10;
            self.path = path;
            [path moveToPoint:btn.center];
            break;
        }
    }
    
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    UITouch *touch = touches.anyObject;
    CGPoint movePoint = [touch locationInView:self];
    self.movePoint = movePoint;
    
    //点击事件开始以后设置按钮状态
    for (UIButton *btn in self.subviews) {
        if (CGRectContainsPoint(btn.frame, movePoint) && !btn.selected) {
            [self.buttonValue appendFormat:@"%ld", btn.tag];
            btn.selected = YES;
            self.selectedMovePoint = btn.center;
            [self.path addLineToPoint:btn.center];
            break;
        }
    }
    [self setNeedsDisplay];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    // 结束之后，取消所有按钮的选中状态
    for (UIButton *btn in self.subviews) {
        if (btn.selected) {
            btn.selected = NO;
        }
    }
    
    // 清除超出按钮之外的尾巴
    self.movePoint = self.selectedMovePoint;
    
    if ([self.delegate respondsToSelector:@selector(lockPassViewOnValue:andPrintScreenImage:)]) {
        [self.delegate lockPassViewOnValue:self.buttonValue andPrintScreenImage:[self printScreenAtSomeRect]];
    }
    NSLog(@"绘制的密码是%@", self.buttonValue);
    
    // 为了保证截图可以得到绘制的密码图案，必须在清空按钮之间路径之前调用代理方法
    // 清空被选中按钮之间的路径
    self.path = nil;
    [self setNeedsDisplay];
    
}

- (UIImage *)printScreenAtSomeRect {
    
    UIGraphicsBeginImageContextWithOptions(self.frame.size, NO, 0.0);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [self.layer renderInContext:ctx];
    UIImage *lockIMG = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsGetCurrentContext();
    
    return lockIMG;
}

- (void)drawRect:(CGRect)rect {
    //绘制超出按钮范围的尾巴
    UIBezierPath *path = [UIBezierPath bezierPath];
    path.lineWidth = 10;
    [path moveToPoint:self.selectedMovePoint];
    [path addLineToPoint:self.selectedMovePoint];
    [[UIColor blackColor] set];
    [self.path stroke];
    [path stroke];
    
}

@end
